package net.sf.eclipseshell.editors;

import java.util.Iterator;
import java.util.Set;

import net.sf.eclipseshell.extensions.ExecutionEnvironmentLaunchManager;
import net.sf.eclipseshell.extensions.IExecutionEnvironment;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.MenuListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.actions.ActionDelegate;

public class ConnectToActionDelegate extends ActionDelegate implements
		IEditorActionDelegate, IMenuCreator, SelectionListener {

	InterpreterEditor editor_;
	
	public ConnectToActionDelegate() {
		super();
		// TODO Auto-generated constructor stub
	}

	public void setActiveEditor(IAction action, IEditorPart targetEditor) {
		// 0TODO Auto-generated method stub
//		super.set
		action.setMenuCreator(this);
		if (targetEditor instanceof InterpreterEditor) {
			editor_ = (InterpreterEditor) targetEditor;
		}
	}
	
	public void run(IAction action) {
		if(editor_ != null){
			editor_.connectTo(null);
		}
	}
	public void selectionChanged(IAction action, ISelection selection) {
		// TODO Auto-generated method stub
		super.selectionChanged(action, selection);
		action.setMenuCreator(this);
//		action.se
//		super.
		
	}
	
	public void dispose() {
		//  Auto-generated method stub
	}
	
	public Menu getMenu(Control parent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public Menu getMenu(Menu parent) {
		// TODO Auto-generated method stub
		Menu m = new Menu(parent);
		Set allActiveExecutionEnvironments = ExecutionEnvironmentLaunchManager.getAllActiveExecutionEnvironments();
		for (Iterator iter = allActiveExecutionEnvironments.iterator(); iter	.hasNext();) {
			IExecutionEnvironment env = (IExecutionEnvironment) iter.next();
//			interpreter_ = env;
			// TODO: HACK!!!! Do the drop down menu bit here!
//			break;
			MenuItem item = new MenuItem(m, SWT.DEFAULT);
			item.setText(env.toString());
			
			// TODO: dangerous: maybe use a WeakReference to prevent a leak!
			Object[] data = new Object[1];
			data[0] = env;
			item.setData(data);
			item.addSelectionListener(this);
		}
//		m.addMenuListener();
//		item.setText("Foo");
//		m.setDefaultItem(item);
//		item.
		return m;
	}

	public void widgetSelected(SelectionEvent e) {
		// TODO Auto-generated method stub
		MenuItem source = (MenuItem) e.getSource();
		if(editor_ != null){
			Object tempData = source.getData();
			if (tempData instanceof Object[]) {
				Object[] tempEnv = (Object[]) tempData;
				IExecutionEnvironment env = (IExecutionEnvironment)tempEnv[0];
				editor_.connectTo(env);
			}

		}
	}

	public void widgetDefaultSelected(SelectionEvent e) {
		// TODO Auto-generated method stub
//		S+ystem.out.println("World");
	}
}
